<?php
// +----------------------------------------------------------------------
// | quickadmin框架 [ quickadmin框架 ]
// +----------------------------------------------------------------------
// | 版权所有 2020~2022 南京新思汇网络科技有限公司
// +----------------------------------------------------------------------
// | 官方网站: https://www.quickadmin.top
// +----------------------------------------------------------------------
// | Author: zs <909883663@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use util\Excel;

/*
 * @Description: index
 * @Date: 2021-05-25 14:57:43
 */

class Index extends BaseController
{





    public function index()
    {
        $data = Excel::import('storage/2.xlsx', 2);
        foreach ($data as $key => $value) {
            if (!empty($value)) {
                $type = $this->key_site(($key));
                foreach ($value as $kl => $info) {
                    if (!empty($info)) {
                        file_put_contents('1.text', $type);
                        file_put_contents('2.text', $info);
                        $rst[] = [
                            'create_time' => excelTime($info[0]),
                            'product_detail' => $info[1],
                            'product_price' => $info[2],
                            'rebate' => $info[3],
                            'amz_price' => $info[4],
                            'other' => $info[5],
                            'site' => $type,
                            'admin_id' => 41,
                        ];
                    }
                }
            }
        }
        halt($rst);
        $data = Excel::import('storage/1.xlsx', 2);
        halt($data);
        $start_time = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month')));
        $end_time = strtotime(date('Y-m-d 23:59:59', strtotime(-date('d') . 'day')));
        $where[]  = ['b.dt_addtime', '>=', $start_time];
        $where[]  = ['b.dt_addtime', '<=', $end_time];
        //徐艳桃  id 26  店铺  75 
        // $old_admin_info = Db::connect('OLD')->table('cmf_user_shop')->where(['up_operate' => 26, 'up_status' => 1])->find();
        $old_admin_info = Db::connect('OLD')->table('cmf_user_shop')
            ->alias('a')
            ->field('SUM(dt_real_cash) as dt_real_cash')
            ->join('cmf_deliver_set b', 'b.dt_shop_id = a.up_id', 'left')
            ->where($where)
            ->where(['a.up_status' => 1, 'a.up_operate' => 26])
            ->select();
        halt($old_admin_info);
        $old_admin = $old_admin_info['id'];
        if (empty($old_admin)) {
            return error('没有查到用户信息');
        }
        // 获取上个月的开始结束时间

        // halt($start_time, $end_time);
        // $end_time = date('Y-m-01 00:00:00', strtotime('-18 month'));
        //交易记录
        $where[]  = ['create_time', '>=', $start_time];
        $where[]  = ['create_time', '<=', $end_time];
        //查询上月的汇率
        $exchage = table('exchange')->where(['create_time' => date('Y-m', strtotime('-1 month'))])->select()->toarray();
        // halt($this->excelTime(45168));

        $data = Excel::import('storage/zhy.xlsx', 2);
        foreach ($data as $key => $value) {
            if (!empty($value)) {
                $type = $this->key_site(($key));
                foreach ($value as $kl => $info) {
                    if (!empty($info)) {
                        file_put_contents('1.text', $info);
                        file_put_contents('2.text', $kl);
                        $rst[] = [
                            'create_time' => excelTime($info[0]),
                            'product_detail' => $info[1],
                            'product_price' => $info[2],
                            'rebate' => $info[3],
                            'amz_price' => $info[4],
                            'other' => $info[5],
                            'site' => $type,
                            'admin_id' => 41,
                        ];
                    }
                }
            }
        }
        halt($rst);
    }

    //提成测试
    public function tichen()
    {
        //方亮  id  98  
        //连表查询用户表和业绩表
        $bonus_info = table('system_admin')
            ->field("a.id, a.is_group_leader,b.site,b.sale,b.amz,b.plat,b.adv,b.product_cost,b.logistics,b.other,b.profit,b.create_time,b.id as b_id")
            ->alias('a')
            ->join('qu_yeji b', 'b.admin = a.id', 'left')
            // ->where($where)
            ->where(['a.id' => 39])
            ->group('a.id')
            ->order('b.create_time desc')
            ->find();
        $profit = $bonus_info['sale'] + $bonus_info['amz'] - $bonus_info['plat'] - $bonus_info['adv'] - $bonus_info['product_cost'] - $bonus_info['logistics'] - $bonus_info['other'];
        //提成小于0 全部置为空
        if ($profit < 0) {
            $data = [
                'reduce' => 0,
                'group_reduce' => 0,
                'bonus' => 0,
            ];
        } else {
            //查看是否是组长
            if ($bonus_info['is_group_leader'] == 1) {
                $data = [
                    'reduce' => $profit > 8000 ? ($profit * 0.04) : 0,
                    'group_reduce' => 0,
                    'bonus' => $this->get_group_bonus($profit),
                ];
            } else {
                $data = [
                    'reduce' => $profit > 8000 ? ($profit * 0.04) : 0,
                    'group_reduce' => $profit > 8000 ? ($profit - 10000) * 0.02 : 0,
                    'bonus' => $this->get_bonus($profit),
                ];
            }
        }

        table('yeji')->where(['id' => $bonus_info['b_id']])->update($data);
        halt($data);
    }

    //运营提成
    public function  get_bonus($profit = 0)
    {
        switch ($profit) {
            case $profit > 8000 && $profit < 20000:
                return 200;
                break;
            case $profit >= 20000 && $profit < 30000:
                return 400;
                break;
            case $profit >= 30000 && $profit < 40000:
                return 600;
                break;
            case $profit >= 40000 && $profit < 100000000:
                return 800;
                break;
            default:
                return  0;
                break;
        }
    }

    //组长提成
    public function  get_group_bonus($profit = 0)
    {
        switch ($profit) {
            case $profit > 10000 && $profit < 30000:
                return 200;
                break;
            case $profit >= 30000 && $profit < 40000:
                return 400;
                break;
            case $profit >= 40000 && $profit < 60000:
                return 600;
                break;
            case $profit >= 60000 && $profit < 100000000:
                return 800;
                break;
            default:
                return  0;
                break;
        }
    }

    //通过下标获取对应的站点类型
    public function key_site($key)
    {
        $data = ['uk', 'de', 'fr', 'it', 'es', 'nl', 'se', 'bi', 'pl', 'us', 'tr'];
        return $data[$key];
    }

    public function ban()
    {
        $data = Excel::import('storage/w.xlsx');
        unset($data[1], $data[2]);
        halt($data);
        foreach ($data as  $value) {
            table('sumskucount')->where(['sku' => strtolower($value[0])])->update(['sum' => $value[3]]);
        }
        return success('更新成功');
    }
}
